package com.cloay.todaytopfunny.activity;

import java.util.List;

import org.apache.http.Header;
import org.json.JSONObject;

import cn.waps.AppConnect;

import com.cloay.todaytopfunny.R;
import com.cloay.todaytopfunny.adapter.CJokeAdapter;
import com.cloay.todaytopfunny.entity.CJoke;
import com.cloay.todaytopfunny.model.CJokeModel;
import com.cloay.todaytopfunny.util.JsonUtil;
import com.cloay.todaytopfunny.util.Util;
import com.cloay.todaytopfunny.util.Util.ActivityFinishAnmationType;
import com.loopj.android.http.JsonHttpResponseHandler;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 * @ClassName: ListJokesActivity 
 * @Description: 段子列表 
 * @author cloay Email:shangrody@gmail.com 
 * @date 2014-10-18 下午3:56:47 
 *
 */
public class ListJokesActivity extends BaseListActivity {
	private CJokeAdapter adapter;
	private String type;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.base_listview_layout);
		type = getIntent().getStringExtra("joke_type");
        init();
        adapter = new CJokeAdapter(this);
        listView.setAdapter(adapter);
        
        dlg.show();
        loadData(true);
        
        if(Util.readStartCount(this) >= 5)
			addAd();
	}
	
	@Override
	protected void loadData(final boolean isRefresh) {
		super.loadData(isRefresh);
		CJokeModel.loadJoke(this, type, page, pageLimit, new JsonHttpResponseHandler(){

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				dlg.dismiss();
				Util.showCLog("list_jokes: " + response);
				if(!response.optString("error").equals("")){
					if(!isRefresh)
						page -= 1;
					int error_code = response.optInt("error_code");
					Util.showToastWithErrorCode(ListJokesActivity.this, response.optString("message", ""), error_code);
				}else{
					List<CJoke> listJoke = JsonUtil.jsonToJokeList(response.optJSONArray("items"));
					Util.showCLog("lll: " +  listJoke.size() + "pageLimit: " + pageLimit);
					if(isRefresh){
						adapter.listJoke.clear();
					}
					adapter.listJoke.addAll(listJoke);
					canLoadMore = adapter.listJoke.size() < response.optInt("total") - 5;
					listView.setCanLoadMore(canLoadMore);
					adapter.notifyDataSetChanged();
				}
				loadEnd(isRefresh);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				dlg.dismiss();
				if(!isRefresh)
					page -= 1;
				loadEnd(isRefresh);
				Util.showCLog("err: " + throwable.getMessage());
				Util.showToastWithErrorCode(ListJokesActivity.this, throwable.getMessage(), statusCode);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				dlg.dismiss();
				if(!isRefresh)
					page -= 1;
				loadEnd(isRefresh);
				Util.showCLog("err: " + throwable.getMessage());
				Util.showToastWithErrorCode(ListJokesActivity.this, throwable.getMessage(), statusCode);
			}
			
		});
	}
		
	@Override
	protected void initEmpty() {
		emptyTextV = (TextView) findViewById(R.id.empty_text_view);
		emptyTextV.setText("亲，没有加载到数据哦！");
	}

	@Override
	protected void onLoadDone() {
		if(adapter.listJoke.size() > 0){
			emptyTextV.setVisibility(View.INVISIBLE);
		}else{
			emptyTextV.setVisibility(View.VISIBLE);
		}
	}

	
	@Override
	protected void initNav() {
		super.initNav();
		String title = "今日糗条";
		if(type.equals("latest")){
			title = "最新糗事";
		}else if(type.equals("imgrank")){
			title = "囧图囧事";
		}
		titleTextV.setText(title);
		titleTextV.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				listView.setSelection(0);
				listView.smoothScrollToPosition(0);
			}
		});
		
		leftBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Util.finishActivityWithAnmationType(ListJokesActivity.this, ActivityFinishAnmationType.ANMATION_LEFT_IN_RIGHT_OUT);
			}
		});
	}
	
	private void addAd(){
		AppConnect.getInstance(this);
		LinearLayout adlayout =(LinearLayout)findViewById(R.id.AdLinearLayout);
		AppConnect.getInstance(this).showBannerAd(this, adlayout);
	}
}
